package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.MessageBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *  信息框-列表 异步请求
 */
public class GetMessageInfoTop extends BaseCommand {

    private int catid = DataCenter.messageInfo_id[0]; //当前分类编号
    private int list_type = 1;
    private String TAG = "GetMessageInfoTop";
    public GetMessageInfoTop(int catid) {
        this.catid = catid;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_MESSAGEINFO_LIST;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("catid", catid  + "");
        paramters.put("list_type",list_type + "");
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetMessageInfoLIst.yes()");

        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONArray jsonArray = jsonObject.optJSONArray("data");

            ArrayList<MessageBean> targetList = DataCenter.messageInfoTop;

            targetList.clear();

            for(int i = 0; i < jsonArray.length(); ++i){ // i从1开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                MessageBean bean = JsonUtil.jsonToObject(o.toString(), MessageBean.class);
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_MESSAGEINFO_TOP_SUCCESS);
    }

}
